package com.hosp.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hosp.bean.Behospital;
import com.hosp.bean.Chargeproject;
import com.hosp.bean.Hosregister;
import com.hosp.bean.Pricepeople;
import com.hosp.controller.dto.BeHosiptalDTO;
import com.hosp.mapper.BehospitalMapper;
import com.hosp.mapper.DoctorMapper;
import com.hosp.mapper.HosregisterMapper;
import com.hosp.mapper.PricepeopleMapper;
import com.hosp.vo.BeHospitalDetailVO;
import com.hosp.vo.BeHospitalVO;
import com.hosp.vo.PageRequestVO;
import com.hosp.vo.RegistrationVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author ztranscript 2021/10/16 8:54
 **/
@Slf4j
@Service
public class BeHospitalService {
    @Autowired
    private DoctorMapper doctorMapper;

    @Autowired
    private HosregisterMapper hosregisterMapper;

    @Autowired
    private BehospitalMapper behospitalMapper;

    @Autowired
    private RegistrationService registrationService;

    @Autowired
    private PricepeopleMapper pricepeopleMapper;

    /**
     * 查询列表
     *
     * @param beHosiptalDTO 查询条件
     * @param pageRequest   分页条件
     * @return 响应
     */
    public PageInfo<BeHospitalVO> list(BeHosiptalDTO beHosiptalDTO, PageRequestVO pageRequest) {
        /*List<Behospital> behospitals = behospitalMapper.selectByExample(null);
        PageInfo<Behospital> behospitalPageInfo = new PageInfo<>(behospitals);

        List<BeHospitalVO> beHospitalVOList = new ArrayList<>();
        for (Behospital behospital : behospitals) {
            BeHospitalVO dto = new BeHospitalVO();
            BeanUtils.copyProperties(behospital, dto);
            // 查找对应的挂号信息
            RegistrationVO registrationVO = registrationService.selectById(behospital.getHosrId());
            dto.setReg(registrationVO);
            beHospitalVOList.add(dto);
        }

        // 分页信息转换
        PageInfo<BeHospitalVO> pageInfo = new PageInfo<>();
        BeanUtils.copyProperties(behospitalPageInfo, pageInfo);
        pageInfo.setList(beHospitalVOList);*/

        // 分页查询
        PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
        List<BeHospitalVO> voList = behospitalMapper.selectDetail(beHosiptalDTO);

        return new PageInfo<>(voList);
    }

    /**
     * 保存
     *
     * @param beHosiptalDTO 保存对象
     * @return 响应
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean save(BeHosiptalDTO beHosiptalDTO) {
        if (Objects.isNull(beHosiptalDTO.getBehId())) {
            beHosiptalDTO.setBehIntime(DateUtil.date());
            behospitalMapper.insertSelective(beHosiptalDTO);
            // 将关联的挂号信息状态改为“已住院”
            Hosregister hosregister = hosregisterMapper.selectByPrimaryKey(beHosiptalDTO.getHosrId());
            hosregister.setHosrState(1);
            registrationService.save(hosregister);
        } else {
            Long behAntecedent = beHosiptalDTO.getBehAntecedent();
            Long money = beHosiptalDTO.getMoney();
            beHosiptalDTO.setBehAntecedent(behAntecedent + money);
            behospitalMapper.updateByPrimaryKeySelective(beHosiptalDTO);
        }
        return true;
    }

    /**
     * 主键查询
     *
     * @param behId 查询条件
     * @return 响应
     */
    public BeHospitalVO selectById(Integer behId) {
        Behospital behospital = behospitalMapper.selectByPrimaryKey(behId);
        // 查询挂号信息
        RegistrationVO registrationVO = registrationService.selectById(behospital.getHosrId());

        BeHospitalVO beHospitalVO = new BeHospitalVO();
        BeanUtils.copyProperties(behospital, beHospitalVO);
        beHospitalVO.setReg(registrationVO);

        return beHospitalVO;
    }

    /**
     * 删除
     *
     * @param uIds 待删除 IDs
     * @return 响应
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(List<Integer> uIds) {
        return true;
    }

    /**
     * 住院管理-保存住院人的收费项目信息
     *
     * @param beHosiptalDTO 保存信息
     * @return 响应
     */
    public boolean cpSave(BeHosiptalDTO beHosiptalDTO) {
        Pricepeople pricepeople = new Pricepeople();
        pricepeople.setBehId(beHosiptalDTO.getBehId());
        pricepeople.setChapId(beHosiptalDTO.getChapId());
        pricepeopleMapper.insertSelective(pricepeople);
        return true;
    }

    /**
     * 住院人详细信息查询(包含收费项目)
     *
     * @param behId       查询条件
     * @param pageRequest 分页条件
     * @return 响应
     */
    public PageInfo<BeHospitalDetailVO> detail(Integer behId, PageRequestVO pageRequest) {
        BeHospitalVO beHospitalVO = this.selectById(behId);
        List<BeHospitalDetailVO> detailVOList = new ArrayList<>();

        // 查询收费项目
        PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
        List<Chargeproject> chargeprojectList = behospitalMapper.selectChargeproject(behId);
        PageInfo<Chargeproject> chargeprojectPage = new PageInfo<>(chargeprojectList);
        // 查询项目总费用
        Long totalPayment = behospitalMapper.selectTotalPayment(behId);

        // 设值
        for (Chargeproject chargeproject : chargeprojectList) {
            BeHospitalDetailVO detailVO = new BeHospitalDetailVO();
            BeanUtils.copyProperties(beHospitalVO, detailVO);

            detailVO.setTotalPayment(totalPayment);
            chargeproject.setChapTime(DateUtil.date());
            detailVO.setChargeproject(chargeproject);
            detailVOList.add(detailVO);
        }
        PageInfo<BeHospitalDetailVO> resPage = new PageInfo<>();
        BeanUtils.copyProperties(chargeprojectPage, resPage);
        resPage.setList(detailVOList);

        return resPage;
    }

    /**
     * 住院结算基础信息查询(包含收费项目+药品费用)
     *
     * @return 响应
     */
    public PageInfo<BeHospitalDetailVO> accountBasic(PageRequestVO pageRequest) {
        PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
        PageInfo<BeHospitalVO> list = this.list(null, pageRequest);
        PageInfo<BeHospitalDetailVO> pageInfo = new PageInfo<>();
        BeanUtils.copyProperties(list, pageInfo, "list");

        List<BeHospitalVO> beHospitalVOList = list.getList();
        List<BeHospitalDetailVO> detailVOList = new ArrayList<>();
        for (BeHospitalVO beHospitalVO : beHospitalVOList) {
            BeHospitalDetailVO detailVO = new BeHospitalDetailVO();
            BeanUtils.copyProperties(beHospitalVO, detailVO);

            Integer behId = beHospitalVO.getBehId();
            // 查询项目总费用
            Long totalPayment
                    = Optional.ofNullable(behospitalMapper.selectTotalPayment(behId)).orElse(0L);
            // 查询药品总费用
            Long drugPayment
                    = Optional.ofNullable(behospitalMapper.selectDrugPayment(behId)).orElse(0L);

            detailVO.setTotalPayment(totalPayment + drugPayment);
            detailVOList.add(detailVO);
        }

        pageInfo.setList(detailVOList);

        return pageInfo;
    }

    /**
     * 住院结算详细信息查询(包含收费项目+药品费用)
     *
     * @param behId 查询条件
     * @return 响应
     */
    public PageInfo<BeHospitalDetailVO> accountDetail(Integer behId, PageRequestVO pageRequest) {
        BeHospitalVO beHospitalVO = this.selectById(behId);
        List<BeHospitalDetailVO> detailVOList = new ArrayList<>();

        PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
        // 查询总收费项(含药品)
        List<Chargeproject> chargeprojectList
                = behospitalMapper.selectChargeprojectWithDrug(behId);
        PageInfo<Chargeproject> chargeprojectPage = new PageInfo<>(chargeprojectList);

        // 查询项目总费用
        Long totalPayment
                = Optional.ofNullable(behospitalMapper.selectTotalPayment(behId)).orElse(0L);
        // 查询药品总费用
        Long drugPayment
                = Optional.ofNullable(behospitalMapper.selectDrugPayment(behId)).orElse(0L);

        // 设值
        for (Chargeproject chargeproject : chargeprojectList) {
            BeHospitalDetailVO detailVO = new BeHospitalDetailVO();
            BeanUtils.copyProperties(beHospitalVO, detailVO);

            detailVO.setTotalPayment(totalPayment + drugPayment);
            detailVO.setChargeproject(chargeproject);
            detailVOList.add(detailVO);
        }
        PageInfo<BeHospitalDetailVO> resPage = new PageInfo<>();
        BeanUtils.copyProperties(chargeprojectPage, resPage);
        resPage.setList(detailVOList);

        return resPage;
    }
}
